(function( $ ){
    $.fn.crHorizontalMenu = function(){
        this.addClass('cr_menu_horizontal');
        this.find('a').click(function(){
            var li = $(this).parent();
            var ul = li.parent();
            var ulChild = li.children('ul');
            if(ulChild.length > 0){
                ul.find('.cr_submenu_active').hide().removeClass('cr_submenu_active');
                ulChild.addClass('cr_submenu_active');
                var parentPos = ul.offset();
                if(ul.hasClass('cr_menu_horizontal')){
                    ulChild.css("top", parentPos.top + ul.height());
                    ulChild.css("left", parentPos.left);
                } else {
                    ulChild.css("top", ul.height());
                    ulChild.css("left", 0);
                }
                ulChild.width(ul.width());
                ulChild.show();
                return false;
            }
        });
    }
})( jQuery );

$(function(){
    $('.navigation').crHorizontalMenu();
    $(document).click(function(event) {
        if ($(event.target).closest(".cr_menu_horizontal").length == 0){
            $('.cr_menu_horizontal .cr_submenu_active').hide();
            console.log('t');
        }
    });

    $('#book_year_of_publication').datepicker();
});